import React from 'react'
// ===================================================================== Temp
import SubmitForm from '#cpt/temp/SubmitForm'
import Controls from '#cpt/temp/Controls'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== private template
const Box 			= $Lazy.load(()=>import('#tp/box/box'))
const PreviewImage 	= $Lazy.state(()=>import('#private/pages/preview-image'))
const $cache 		= $Lazy.file(()=>import('#private/cache-api'))
const CheckboxGroup = $Lazy.load(()=>import('@antd/form/checkboxGroup'))
// ===================================================================== component
const defaultsData = [
	{label: '采样日期', value: 'samp_date'},
	{label: '采样时间', value: 'samp_time'},
	{label: '送检科室', value: 'department_name'},
	{label: '送检医生', value: 'doctor'},
]
// ===================================================================== data
const Defaults = function() {
	return <div className='mb5 fx'>
		<span style={{width: 85}} className='tr'>是否记忆：</span>
		<CheckboxGroup nameStr='label' value={Object.keys($fn.local('double-entry-defaults') || {})}  onChange={val => {
			const oldData = $fn.local('double-entry-defaults')
			const newData = {}
			if (oldData) {
				val.forEach(v => {
					newData[v] = oldData[v] ?? ''
				})
			} else {
				val.forEach(v => {
					newData[v] = ''
				})
			}
			$fn.local('double-entry-defaults', newData)
		}} data={defaultsData} />
	</div>
}

class Index extends React.Component{
	state = {
		isDisabled: true,	// 是否禁用表单的按钮
		parentData:{},	// 父级传来的表单数据
		customHide:true,	// 控制自定义属性显示或隐藏
		formData: [
			{ label:'条形码', 		name:'spec_code', 			full:true,	disabled:true,	required:true,	itemClassName:'doubleInfoSpec' },
			{ label:'送检医院', 		name:'hosp_name', 			full:true,	disabled:true},
			{ label:'姓名', 			name:'patient_name', 		half:true,	required:true,	autoFocus:true,		onValue:()=>setTimeout(()=>this.props.setDiffClass(),0) },
			{ label:'性别', 			name:'sex',	value:'45703',	half:true, 	type:'select',	idStr: 'code',	isKeydown: 'sex', 	 onValue: ({rows})=>{
				setTimeout(()=>this.props.setDiffClass(),0)
				if(rows)this.submit.sex_name = rows.name
			}},
			{ label:'年龄一', 		name:'age_first_val', 		half:true,		onValue:()=>setTimeout(()=>this.props.setDiffClass(),0),		outer:true},
			{ label:'年龄类型一', 	name:'age_first_type', 		half:true, 	type:'select',	idStr: 'code',	value: "30001",	onValue: ({rows})=>{
				setTimeout(()=>this.props.setDiffClass(),0)
				if(rows)this.submit.age_first_type_name = rows.name
			}},
			{ label:'年龄二', 		name:'age_second_val', 		half:true,		onValue:()=>setTimeout(()=>this.props.setDiffClass(),0)},
			{ label:'年龄类型二', 	name:'age_second_type', 	half:true, 	type:'select',	idStr: 'code', 	value: "30003",	onValue: ({rows})=>{
				setTimeout(()=>this.props.setDiffClass(),0)
				if(rows)this.submit.age_second_type_name = rows.name
			}},
			{ label:'门诊/住院号', 	name:'outpatient', 			half:true,		onValue:()=>setTimeout(()=>this.props.setDiffClass(),0)},
			{ label:'床号', 			name:'bed_no', 				half:true,		onValue:()=>setTimeout(()=>this.props.setDiffClass(),0)},
			{ label:'送检科室', 		name:'department_name', 	half:true,		onValue:()=>setTimeout(()=>this.props.setDiffClass(),0)},
			{ label:'送检医生', 		name:'doctor', 				half:true,		onValue:()=>setTimeout(()=>this.props.setDiffClass(),0)},
			{ label:'医院标识', 		name:'hosp_identification', full:true,		onValue:()=>setTimeout(()=>this.props.setDiffClass(),0)},
			{ label:'医生电话', 		name:'doctor_phone', 		half:true,		onValue:()=>setTimeout(()=>this.props.setDiffClass(),0)},
			{ label:'受检人电话', 	name:'patient_phone', 		half:true,		onValue:()=>setTimeout(()=>this.props.setDiffClass(),0)},
			{ label:'采样日期',		name:'samp_date',  			half:true, 	type:'date-time', format:1,	submit:1,	showToday:false,	onValue:({value,formData})=>{
				setTimeout(()=>this.props.setDiffClass(),0)
				if(value){
					$fn.disabledRows(formData, 'samp_time', false)
					this.setState({ formData })
				}
			} },
			{ label:'采样时间',		name:'samp_time',			half:true, 	type:'time',		onValue:()=>setTimeout(()=>this.props.setDiffClass(),0)},
			{ label:'备注', 			name:'remark', 				full:true,	type:'textarea',		onValue:()=>setTimeout(()=>this.props.setDiffClass(),0)},
			{ label:'标本性状', 		name:'spec_traits', 		half:true,	type:'select',	idStr: 'code',	onValue: ({rows})=>{
				setTimeout(()=>this.props.setDiffClass(),0)
				if(rows)this.submit.spec_traits_name = rows.name
			}},
			{ label:'采集类型',		name: 'collect_type', 		half:true, 	type: 'radio',	size: 'small',	mode:'button',		onValue:()=>setTimeout(()=>this.props.setDiffClass(),0)},
			{ label:'证件类型', 		name: 'card_type', 			full:true, type:'select', value: '87007', idStr: 'code',		onValue:()=>setTimeout(()=>this.props.setDiffClass(),0) },
			{ label:'证件号',		name: 'card_no',  			full:true,		onValue:()=>setTimeout(()=>this.props.setDiffClass(),0) },
			{ label:'物流人员', 		name:'real_name', 			half:true,	disabled:true},
			{ label:'受检人类型', 	name:'case_type', 			half:true,	type:'select',	idStr: 'code',	isKeydown: 'case_type',		onValue:()=>setTimeout(()=>this.props.setDiffClass(),0) },
			{ label:'病理固定时间',	name:'bingli_coll_time',	full:true, 	type:'date-time',	format:2,		onValue:()=>setTimeout(()=>this.props.setDiffClass(),0)},
			{ label:'病理离体时间',	name:'bingli_take_time',	full:true, 	type:'date-time',	format:2,		onValue:()=>setTimeout(()=>this.props.setDiffClass(),0) },
			{ label:'临床诊断', 		name:'diagnosis_info', 		full:true,	type:'textarea',		onValue:()=>setTimeout(()=>this.props.setDiffClass(),0)},
			{ label:'识别标签',		name: 'tags', 				full:true, 	type: 'checkboxGroup', idStr:'code',		onValue:()=>setTimeout(()=>this.props.setDiffClass(),0) },
			// 病人基本信息 增加5个自定义字段 2021-10-15  --马婷
			{ hide:true,	custom:true, onClick:()=>{this.customClick()}},
			{ label:'备用1', 		name:'user_defined1', 				full:true,	hide:true,		onValue:()=>setTimeout(()=>this.props.setDiffClass(),0) },
			{ label:'备用2', 		name:'user_defined2', 				full:true,	hide:true,		onValue:()=>setTimeout(()=>this.props.setDiffClass(),0) },
			{ label:'备用3', 		name:'user_defined3', 				full:true,	hide:true,		onValue:()=>setTimeout(()=>this.props.setDiffClass(),0) },
			{ label:'备用4', 		name:'user_defined4', 				full:true, 	hide:true,		onValue:()=>setTimeout(()=>this.props.setDiffClass(),0) },
			{ label:'备用5', 		name:'user_defined5', 				full:true, 	hide:true,		onValue:()=>setTimeout(()=>this.props.setDiffClass(),0) },
			{ label:'备用6', 		name:'user_defined6', 				full:true, 	hide:true,		onValue:()=>setTimeout(()=>this.props.setDiffClass(),0) },
			{ label:'备用7', 		name:'user_defined7', 				full:true, 	hide:true,		onValue:()=>setTimeout(()=>this.props.setDiffClass(),0) },
			{ label:'备用8', 		name:'user_defined8', 				full:true, 	hide:true,		onValue:()=>setTimeout(()=>this.props.setDiffClass(),0) },
			{ label:'备用9', 		name:'user_defined9', 				full:true, 	hide:true,		onValue:()=>setTimeout(()=>this.props.setDiffClass(),0) },
			{ label:'备用10', 		name:'user_defined10', 				full:true, 	hide:true,		onValue:()=>setTimeout(()=>this.props.setDiffClass(),0) },
		]
	}
	model = {}
	query = {}
	submit = {
		page_source: 'sp'
	}
	
	componentDidMount(){
		this.getSelect()
	}
	
	// 点击展开或关闭自定义属性
	customClick = () => {
		const { customHide, formData } = this.state
		this.setState({customHide:!customHide},()=>{
			$fn.hideRows(formData,['user_defined1','user_defined2','user_defined3','user_defined4','user_defined5','user_defined6','user_defined7','user_defined8','user_defined9','user_defined10'], this.state.customHide)
			this.setState({formData})
		})
	}
	
	// 父级调用 渲染初始数据
	setInitValue = (data) => {
		if ($fn.hasObject(data)) {
			const { formData } = this.state
			// 采样日期不能大于接收日期
			const sampDate = $fn.getRow( formData, 'samp_date')
			const { check_time } = data
			if( check_time ){
				const check = check_time.split(' ')
				sampDate.defaultPickerValue = check[0]
				sampDate.after = check[0]
			}
			if(!data.samp_date){
				$fn.disabledRows(formData, 'samp_time', true)
			}else{
				$fn.disabledRows(formData, 'samp_time', false)
			}
			// 初始化表单
			this.formRef.reset()
			// 表单写入默认值
			this.formRef.setValue(data)
			if(!this.props.noImg){
				if(data.img_url) {
					this.props.previewImage(data.img_url)
				}else {
					this.props.previewImage('')
				}
			}
			// 更新状态
			this.setState({ parentData: data, isDisabled: false })
		} else {
			this.formRef.reset()
			this.setState({ parentData: {}, isDisabled: true })
		}
	}
	// 获取下拉数据
	getSelect = () => {
		const { formData } = this.state
		
		$cache.then( async f => {
			const dic = await f.getDicCache({ code: [ 45700, 21000, 42000, 69700, 30000, 87000 ]})
			// 标本性状
			$fn.setRow( formData, 'spec_traits', dic['21000'] )
			// 受检人类型
			$fn.setRow( formData, 'case_type', dic['42000'] )
			// 性别
			$fn.setRow( formData, 'sex', dic['45700'] )
			//识别标签
			$fn.setRow( formData, 'tags', dic['69700'] )
			// 年龄类型
			$fn.setRow( formData, 'age_first_type', dic['30000'] )
			$fn.setRow( formData, 'age_second_type', dic['30000'] )
			// 采集类型
			$fn.setRow( formData, 'collect_type', f.collect_type )
			// 身份证类型 [2021-8-17 3:19:24 ?F10: PM?] --伍智睿
			$fn.setRow( formData, 'card_type', dic['87000'])
			
			this.setState({ formData })
		})
	}
	
	controls = () => {
		const { isDisabled } = this.state
		const { type,input_status,code } = this.props
		// 保存 查看图片 审核 编辑
		return [
			+type===1 && Controls.submit.call(this, '保存', 'spec-case-info/keep',{
				onOk: () => {
					this.props.onRefresh()
				},
				param: () => {
					const d = this.formRef.getValue()
					const data = $fn.local('double-entry-defaults')
					if(!d.samp_date ) d.samp_date = ''
					if(!d.samp_time ) d.samp_time = ''
					d.coll_time =  d.samp_date + ' '+ d.samp_time
					d.entry_type = +input_status===1? 'double_left':'double_right'
					Object.keys(data).forEach(v => {
						data[v] = d[v]
					})
					$fn.local('double-entry-defaults', data)
					return { ...this.state.parentData, ...d,double_model:input_status }
				},
				disabled: isDisabled, code
			}),
			+type===2 && Controls.submit.call(this,'以此为准', 'spec-case-info/pass',{
				onOk: () => this.props.onRefresh(),
				param: () => {
					const d = this.formRef.getName()
					if(!d.samp_date ) d.samp_date = ''
					if(!d.samp_time ) d.samp_time = ''
					d.coll_time =  d.samp_date + ' '+ d.samp_time
					d.entry_type = +input_status===1? 'double_left':'double_right'
					// status 如果记录表无此状态，手动设置；	1：待审核
					return { status: 1, page_source: 'double_entry', ...this.state.parentData, ...d }
				},
				disabled: isDisabled
			})
		]
	}
	
	reset = () => this.formRef?.reset()
	
	render(){
		const { formData, isDisabled, customHide, parentData } = this.state
		const { disabled, opeName, status } = this.props
		return (
			<>
				<Box title={this.props.title} full style={{ width: this.props.width || 450 }} className='pr10 h' contentClassName='mt10 infoForm' flex scroll controls={this.controls()} >
					{ this.props.entry ? Defaults.call(this) : ''}
					{ (status === 3 || opeName) ? <div className='mb5 fx'><span style={{width: 85}} className='tr mr15'>录入人：</span>{parentData.operator_name}</div> : '' }
					{ SubmitForm.call(this,{ data:formData, disabled: disabled?disabled:isDisabled, labelWidth:'s6', noholder:true, customHide } ) }
				</Box>
				<PreviewImage onRef = { ref => this.previewRef = ref } />
			</>
		)
	}
}

export default Index